//
//  Extension+UIImage.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/28.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

extension UIImage{
    
    //截屏
    static func getScreenShot()->UIImage{
        
        let windows = UIApplication.sharedApplication().keyWindow!
        
        UIGraphicsBeginImageContext(windows.bounds.size)
        
        windows.drawViewHierarchyInRect(windows.bounds, afterScreenUpdates: false)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image
    }
    
    //压缩图片
    
    func getImageScale(width: CGFloat) -> UIImage{
     
        if self.size.width < width {
            
            return self
        }else{
            
            let height = width * self.size.height / self.size.width
            
            let rect = CGRectMake(0, 0, width, height)
            
            UIGraphicsBeginImageContext(rect.size)
            
            self.drawInRect(rect)
            
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return image
        }
        
    }
    // 获得emotion 的image
    class func getEmoticonImage(path: String?) -> UIImage?{
    
        return UIImage(named: path ?? "", inBundle: SHEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
    }
}
